<template>
  <page-layout>
    <a-spin :spinning="isLoading">
      <a-card :title="title">
        <template #extra>
          <a-button v-for="(btn, btnIndex) in btnGroups"
                    :key="btnIndex"
                    type="primary"
                    class="ml-5"
                    @click="handleBtnClick(btn)"
          >
            {{ btn.title }}
          </a-button>
        </template>

        <a-form>
          <a-row type="flex" justify="space-between">
            <a-col :span="7" v-for="item in baseFields.fields" :key="item.field">
              <a-form-item :label="item.title" class="mb-5">
                {{ handleFormEmptyData(baseFields.data[item.field]) }}
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <a-tabs :active-key(v-model)="activeKey" size="small">
          <a-tab-pane key="detail" tab="详情">
            <DetailBaseForm :data="baseFields.data"/>
          </a-tab-pane>
        </a-tabs>
      </a-card>
    </a-spin>
  </page-layout>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { handleFormEmptyData } from '@/tools/filters'
import { getDictByCodesReturnObj } from '@/tools/get-dict'
import { isEmpty } from '@/tools/common'
import { selectById } from '@/api/module/school'
import { message } from 'ant-design-vue'
import DetailBaseForm from './components/detail-base-form'
import dictEnum from "@/const/dict"

export default {
  components: {
    DetailBaseForm
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const activeKey = ref('detail')
    const title = ref('学校详情')
    const isLoading = ref(false)
    const btnGroups = reactive([
      // {
      //   title: '新增联系人',
      //   permissionCode: 'university:school:edit',
      //   atClick: () => {
      //     router.push('/university/contact-manage')
      //   }
      // },
      {
        title: '修改',
        permissionCode: 'university:school:edit',
        atClick: () => {
          router.push({
            path: '/university/college-manage/edit',
            query: route.query
          })
        }
      },
      // {
      //   title: '签约',
      //   permissionCode: 'university:school:edit',
      //   atClick: () => {
      //   }
      // },
      // {
      //   title: '更改进度',
      //   permissionCode: 'university:school:edit',
      //   atClick: () => {
      //   }
      // },
      // {
      //   title: '更改负责人',
      //   permissionCode: 'university:school:edit',
      //   atClick: () => {
      //   }
      // },
    ])
    const baseFields = reactive({
      fields: [
        { title: '学校名称', field: 'schoolCnName' },
        { title: '负责人', field: 'schResponsiblePerson' },
        { title: '跟进进度', field: 'schFollowUpProgress' },
        { title: '学校编号', field: 'schNo' },
        { title: '创建人', field: 'createBy' },
        { title: '创建时间', field: 'createTime' },
      ],
      data: {},
      dict: {}
    })

    const handleBtnClick = btn => {
      btn.atClick && btn.atClick(btn)
    }

    const getDataByDict = (data, dictKey) => {
      if (isEmpty(data)) return '-'
      const item = baseFields.dict[dictKey].find(v => v.value == data)
      if (isEmpty(item)) return '-'
      return item.label
    }

    onMounted(async () => {
      isLoading.value = true

      baseFields.dict = await getDictByCodesReturnObj([
        dictEnum.YES_OR_NO,
        dictEnum.SCH_TYPE,
        dictEnum.SCH_SYSTEM,
        dictEnum.SCH_NATURE,
        dictEnum.FOLLOW_PROGRESS,
        dictEnum.Intention_PROJECT,
        dictEnum.TRANSFER_POLICY
      ])

      let res = await selectById({ id: route.query.id })
      if (res.code === 500) return message.error('获取详情失败')

      res.data.files.map(file => res.data[file.type] = file.name)
      res.data.schNature = getDataByDict(res.data.schNature, dictEnum.SCH_NATURE)
      res.data.schCategory = getDataByDict(res.data.schCategory, dictEnum.SCH_TYPE)
      res.data.schCreditTransferPolicy = getDataByDict(res.data.schCreditTransferPolicy, dictEnum.TRANSFER_POLICY)
      res.data.schCompetitorYesno = getDataByDict(res.data.schCompetitorYesno, dictEnum.YES_OR_NO)
      res.data.schOnlineYesno = getDataByDict(res.data.schOnlineYesno, dictEnum.YES_OR_NO)
      res.data.schSchoolSystem = getDataByDict(res.data.schSchoolSystem, dictEnum.SCH_SYSTEM)
      res.data.schIntendedProject = getDataByDict(res.data.schIntendedProject, dictEnum.Intention_PROJECT)
      res.data.schFollowUpProgress = getDataByDict(res.data.schFollowUpProgress, dictEnum.FOLLOW_PROGRESS)

      baseFields.data = res.data
      isLoading.value = false
    })

    return {
      isLoading,
      title,
      btnGroups,
      baseFields,
      activeKey,
      handleBtnClick,
      handleFormEmptyData
    }
  }
}
</script>
